{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from src import config, utils\n",
    "import logging\n",
    "from sklearn.model_selection import KFold\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Will drop ['a_feature', 'UserInfo_270']\n",
      "WARNING:root:Will select ['flag', 'UserInfo_82', 'UserInfo_222', 'UserInfo_242', 'UserInfo_130', 'UserInfo_149', 'UserInfo_13', 'UserInfo_262', 'UserInfo_197', 'UserInfo_27', 'UserInfo_40', 'UserInfo_109', 'UserInfo_100', 'UserInfo_203', 'ProductInfo_47', 'UserInfo_253', 'UserInfo_16', 'UserInfo_37', 'UserInfo_28', 'UserInfo_147', 'UserInfo_113', 'ProductInfo_151', 'ProductInfo_182', 'UserInfo_134', 'UserInfo_12', 'ProductInfo_90', 'ProductInfo_89', 'UserInfo_108', 'UserInfo_76', 'ProductInfo_49', 'ProductInfo_48', 'ProductInfo_35', 'UserInfo_38', 'ProductInfo_27', 'UserInfo_17', 'UserInfo_105']\n"
     ]
    }
   ],
   "source": [
    "assert config.dataset == 'val'\n",
    "train = pd.read_csv(config.pj_root + 'data/' + config.dataset + '.csv', index_col='no')\n",
    "\n",
    "assert train.columns[0] == 'flag'\n",
    "a_feature = pd.read_csv(config.pj_root + 'data/a_feature.csv', index_col='no')\n",
    "train = train.join(a_feature)\n",
    "\n",
    "if len(config.drop_columns) != 0:\n",
    "    logging.warning('Will drop %s' % str(config.drop_columns))\n",
    "    train = train.drop(config.drop_columns, axis=1)\n",
    "\n",
    "if len(config.select_columns) != 0:\n",
    "    config.select_columns.insert(0, 'flag')  # use flag in training\n",
    "    logging.warning('Will select %s' % str(config.select_columns))\n",
    "    train = train[config.select_columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:basic_process all_columns....\n"
     ]
    }
   ],
   "source": [
    "train, col_func_map = utils.basic_process(train, has_flag=True)\n",
    "utils.dump_to_data(col_func_map, 'col_func_map.pkl')\n",
    "X = train.values[:, 1:]\n",
    "Y = train.values[:, 0:1].reshape((-1,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "config.kfold_k=4\n",
    "\n",
    "config.model_para =  {\n",
    "#     'min_child_weight': 125, \n",
    "    'gamma': 0.0, \n",
    "    'objective': 'rank:pairwise', \n",
    "#     'subsample': 0.75, \n",
    "    'max_depth': 3, \n",
    "    'learning_rate': 0.1, \n",
    "    'n_estimators': 65,\n",
    "    'scale_pos_weight': 1\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Use 4 Folds...\n",
      "INFO:root:{'learning_rate': 0.1, 'scale_pos_weight': 1, 'n_estimators': 65, 'gamma': 0.0, 'max_depth': 3, 'objective': 'rank:pairwise'}\n",
      "INFO:root:Fold 1/4 Score: 0.623064 \n",
      "INFO:root:Fold 2/4 Score: 0.513513 \n",
      "INFO:root:Fold 3/4 Score: 0.573040 \n",
      "INFO:root:Fold 4/4 Score: 0.606567 \n",
      "INFO:root:Avg score 0.579046. \n"
     ]
    }
   ],
   "source": [
    "logging.info('Use %d Folds...' % config.kfold_k)\n",
    "logging.info(config.model_para)\n",
    "kf = KFold(n_splits=config.kfold_k)\n",
    "all_score = 0\n",
    "importance = []\n",
    "for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = Y[train_index], Y[test_index]\n",
    "    model = config.model(**config.model_para)\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict_proba(X_test)\n",
    "    score = utils.report(y_test, y_pred[:, 1])\n",
    "    all_score += score\n",
    "    logging.info('Fold %d/%d Score: %f ' % (i + 1, config.kfold_k, score))\n",
    "    with open(config.pj_root + ('model/%s.mo%d' % (config.model.__name__, i)), 'wb') as f:\n",
    "        pickle.dump(model, f)\n",
    "logging.info('Avg score %f. ' % (all_score / config.kfold_k))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>feature</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>UserInfo_82</td>\n",
       "      <td>0.229630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>395</th>\n",
       "      <td>UserInfo_222</td>\n",
       "      <td>0.162963</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>353</th>\n",
       "      <td>UserInfo_197</td>\n",
       "      <td>0.081481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>429</th>\n",
       "      <td>UserInfo_242</td>\n",
       "      <td>0.066667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>UserInfo_13</td>\n",
       "      <td>0.059259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>UserInfo_27</td>\n",
       "      <td>0.044444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>ProductInfo_47</td>\n",
       "      <td>0.037037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>451</th>\n",
       "      <td>UserInfo_253</td>\n",
       "      <td>0.037037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>114</th>\n",
       "      <td>ProductInfo_49</td>\n",
       "      <td>0.029630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>273</th>\n",
       "      <td>UserInfo_153</td>\n",
       "      <td>0.029630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>172</th>\n",
       "      <td>UserInfo_100</td>\n",
       "      <td>0.022222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>265</th>\n",
       "      <td>UserInfo_149</td>\n",
       "      <td>0.022222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>UserInfo_203</td>\n",
       "      <td>0.022222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>UserInfo_50</td>\n",
       "      <td>0.014815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>ProductInfo_35</td>\n",
       "      <td>0.014815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>202</th>\n",
       "      <td>UserInfo_113</td>\n",
       "      <td>0.014815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>ProductInfo_148</td>\n",
       "      <td>0.014815</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463</th>\n",
       "      <td>UserInfo_257</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>335</th>\n",
       "      <td>ProductInfo_146</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>UserInfo_10</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>201</th>\n",
       "      <td>ProductInfo_89</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>257</th>\n",
       "      <td>UserInfo_144</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>235</th>\n",
       "      <td>UserInfo_130</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>UserInfo_92</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>UserInfo_49</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>UserInfo_40</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69</th>\n",
       "      <td>UserInfo_37</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>UserInfo_28</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>UserInfo_16</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>470</th>\n",
       "      <td>UserInfo_262</td>\n",
       "      <td>0.007407</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             feature  importance\n",
       "144      UserInfo_82    0.229630\n",
       "395     UserInfo_222    0.162963\n",
       "353     UserInfo_197    0.081481\n",
       "429     UserInfo_242    0.066667\n",
       "22       UserInfo_13    0.059259\n",
       "45       UserInfo_27    0.044444\n",
       "109   ProductInfo_47    0.037037\n",
       "451     UserInfo_253    0.037037\n",
       "114   ProductInfo_49    0.029630\n",
       "273     UserInfo_153    0.029630\n",
       "172     UserInfo_100    0.022222\n",
       "265     UserInfo_149    0.022222\n",
       "364     UserInfo_203    0.022222\n",
       "88       UserInfo_50    0.014815\n",
       "74    ProductInfo_35    0.014815\n",
       "202     UserInfo_113    0.014815\n",
       "341  ProductInfo_148    0.014815\n",
       "463     UserInfo_257    0.007407\n",
       "335  ProductInfo_146    0.007407\n",
       "15       UserInfo_10    0.007407\n",
       "201   ProductInfo_89    0.007407\n",
       "257     UserInfo_144    0.007407\n",
       "235     UserInfo_130    0.007407\n",
       "163      UserInfo_92    0.007407\n",
       "87       UserInfo_49    0.007407\n",
       "73       UserInfo_40    0.007407\n",
       "69       UserInfo_37    0.007407\n",
       "47       UserInfo_28    0.007407\n",
       "29       UserInfo_16    0.007407\n",
       "470     UserInfo_262    0.007407"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = pd.DataFrame({'feature':train.columns[1:], 'importance':model.feature_importances_})\n",
    "i = i[i.importance>0].sort_values('importance', ascending=False)\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(config.pj_root + 'model/%s.mo' % config.model.__name__, 'wb') as f:\n",
    "    pickle.dump(model, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Use 2 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.582638. \n",
      "INFO:root:Use 3 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.589325. \n",
      "INFO:root:Use 4 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.573505. \n",
      "INFO:root:Use 5 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.582679. \n",
      "INFO:root:Use 6 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.570142. \n",
      "INFO:root:Use 7 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.581308. \n",
      "INFO:root:Use 8 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.574404. \n",
      "INFO:root:Use 9 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.576708. \n",
      "INFO:root:Use 10 Folds...\n",
      "INFO:root:{'max_depth': 4, 'objective': 'rank:pairwise', 'min_child_weight': 96, 'subsample': 1, 'learning_rate': 0.1, 'gamma': 0, 'n_estimators': 20}\n",
      "INFO:root:Avg score 0.583878. \n"
     ]
    }
   ],
   "source": [
    "#  把各个fold保存为dataframe的形式\n",
    "\n",
    "avg = pd.DataFrame()\n",
    "\n",
    "avg_score = []\n",
    "for k in range(2, 11):\n",
    "    config.kfold_k=k\n",
    "    config.model_para ={'gamma': 0, 'objective': 'rank:pairwise', 'learning_rate': 0.1, 'subsample': 1, 'min_child_weight': 96, 'n_estimators': 20, 'max_depth': 4} \n",
    "    logging.info('Use %d Folds...' % config.kfold_k)\n",
    "    logging.info(config.model_para)\n",
    "    kf = KFold(n_splits=config.kfold_k)\n",
    "    all_score = 0\n",
    "    for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "        X_train, X_test = X[train_index], X[test_index]\n",
    "        y_train, y_test = Y[train_index], Y[test_index]\n",
    "        model = config.model(**config.model_para)\n",
    "        model.fit(X_train, y_train)\n",
    "        y_pred = model.predict_proba(X_test)\n",
    "        score = utils.report(y_test, y_pred[:, 1])\n",
    "        all_score += score\n",
    "    logging.info('Avg score %f. ' % (all_score / config.kfold_k))\n",
    "    avg_score.append(all_score / config.kfold_k)\n",
    "avg = pd.concat([avg, pd.DataFrame(avg_score, index=range(2, 11))], axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
